<?php

namespace app\common\business;

use app\common\model\mysql\ScreenIndicators;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;

class Indicators {

    public $model = null;

    public function __construct()
    {
        $this->model = new ScreenIndicators();
    }

    /**
     * 获取分页列表
     * @throws Exception
     */
    public function getTableList($page=null,$limit=null,$keyword=''): array
    {
        $where=[];
        $data=$this->model->getTableList($where,$page,$limit);
        $category=(new Category())->categoryDictionaries();
        $numberField=['total','talent_proportion','money','city_money','party','enterprise','technology','high_skill','rural','social_work','total_rate','money_rate','city_money_rate','party_rate','enterprise_rate','technology_rate','high_skill_rate','rural_rate','social_work_rate'];
        foreach ($data['list'] as $k=>$v){
            foreach ($numberField as $v1){
                if(isset($v[$v1])){
                    $data['list'][$k][$v1]=floatval($v[$v1]);
                }
            }
            $data['list'][$k]['area_name']= $category[$v['area']] ?? '-';
        }
        return $data;
    }

    /**
     * 新增/保存数据
     * @param $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException|Exception
     */
    public function save($data): bool
    {
        if(empty($data['id'])){
            unset($data['id']);
        }
        return $this->model->saveData($data);
    }

    /**
     * 根据id删除
     * @param $id
     * @return bool
     * @throws Exception
     */
    public function deleteById($id): bool
    {
        if(empty($id)){
            throw new Exception('参数错误');
        }
        return $this->model->deleteById($id);
    }

    /**
     * 根据id修改状态
     * @param $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function updateStatus($data): bool
    {
        if(empty($data['id'])){
            throw new Exception('参数错误');
        }
        return $this->model->updateStatus($data);
    }
}
